package com.cate.controller.admin;

import com.cate.constants.RedisConstant;
import com.cate.exception.BusinessException;
import com.cate.result.RestResponse;
import com.cate.result.SystemCode;
import com.cate.utils.RedisUtil;
import com.cate.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @CreateTime: 2023/7/30
 * @Author: <==lhd==>
 * @Description: 店铺管理
 */
@RestController
@Slf4j
@RequestMapping("/admin/shop")
@Api(tags = "店铺管理")
public class ShopController {

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/status")
    @ApiOperation("获取营业状态")
    public RestResponse<Integer> getStatus(){
        String redisStatus = redisUtil.get(RedisConstant.SHOP_STATUS_KEY);
        log.info("获取营业状态={}",redisStatus);
        return ResultUtil.success(Integer.valueOf(redisStatus));
    }

    @PutMapping("/{status}")
    @ApiOperation("设置营业状态")
    public RestResponse setStatus(@PathVariable("status") Integer status){
        redisUtil.setNoExpireTime(RedisConstant.SHOP_STATUS_KEY,status.toString());
        final String redisStatus = redisUtil.get(RedisConstant.SHOP_STATUS_KEY);
        log.info("设置营业状态={}",redisStatus);
        if(Objects.equals(redisStatus, status.toString())){
            return ResultUtil.success("");
        }else{
            throw new BusinessException(SystemCode.REQUEST_ERROR,"设置营业状态失败");
        }
    }
}
